package cn.hill4j.tool.spring.ext.mvc.context;

import java.util.Objects;
/**
  * ClassName: RequestContextUtils
  * Description: 请求参数上下文处理工具类
  * @author hillchen
  * Date: 2023/8/13
  * @version: 1.0
  */
public class RequestContextUtils {
    private static ThreadLocal<DecRequestContext> REQ_CONTEXT = new ThreadLocal<>();
    /**
     * @author hillchen
     * Description: 获取请求参数上下文
     * Date: 2023/8/13 00:21
     * @return DecRequestContext
     **/
    public static DecRequestContext getReqContext(){
        DecRequestContext context = REQ_CONTEXT.get();
        if (Objects.isNull(context)){
            context  = new DecRequestContext();
            REQ_CONTEXT.set(context);
        }
        return context;
    }
    /**
     * @author hillchen
     * Description: 清除请求参数上下文
     * Date: 2023/8/13 00:22
     **/
    public static void cleanContext(){
        REQ_CONTEXT.remove();
    }
    /**
     * @author hillchen
     * Description: 重置请求body
     * Date: 2023/8/13 00:22
     * @param body 请求body
     **/
    public static void resetReqBody(String body){
        DecRequestContext context = getReqContext();
        context.getMidVal().put(ReqParamKey.buildReqBodyKey(),body);
        context.setBodyReset(true);
    }
    /**
     * @author hillchen
     * Description: 重置req query请求参数
     * Date: 2023/8/13 00:23
     * @param name req query请求参数名
     * @param value req query请求参数值
     **/
    public static void resetReqQuery(String name,String value){
        DecRequestContext context = getReqContext();
        context.getMidVal().put(ReqParamKey.buildReqQueryKey(name),value);
        context.setQueryReset(true);
    }
    /**
     * @author hillchen
     * Description: 获取请求body
     * Date: 2023/8/13 00:23
     * @return String
     **/
    public static String getResetBody(){
        DecRequestContext context = getReqContext();
        return context.getMidVal().get(ReqParamKey.buildReqBodyKey());
    }
    /**
     * @author hillchen
     * Description: req query请求参数值
     * Date: 2023/8/13 00:24
     * @param name req query请求参数名
     * @return String
     **/
    public static String getResetQuery(String name){
        DecRequestContext context = getReqContext();
        return context.getResetQuery(name);
    }
}
